<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Generator</title>
</head>

<body>
    <script>
        function gen$(_context) {
            while (1) {
                switch (_context.prev = _context.next) {
                    case 0:
                        _context.next = 2
                        return 'result1'

                    case 2:
                        _context.next = 4
                        return 'result2'
                    case 4:
                        _context.next = 6
                        return 'result3'
                    case 6:
                    case 'end':
                        return _context.stop()
                }
            }
        }

        var context = {
            next: 0,
            prev: 0,
            done: false,
            stop: function stop() {
                this.done = true
            }
        }

        let gen = function () {
            return {
                next: function () {
                    value = context.done ? undefined : gen$(context)
                    done = context.done
                    return {
                        value,
                        done
                    }
                }
            }
        }

        function run() { // 自动执行器
            var g = gen()
            function _next(val) {
                var res = g.next();
                console.log(res);
                if (res.done) return res.value;
                _next(res.value)
            }

            _next()
        }

        run(gen)
    </script>
</body>

</html>